// pages/recommend/recommend.js
const app=getApp();
const { post } = require("../../utils/request");
const request=require("../../utils/request")
Page({

    /**
     * 页面的初始数据
     */
    data: {
      banners:[
       {image:"https://oss-xpc0.xpccdn.com/newupload/assets/article/cover/2022/4/62475cf4a574a@1125w_633h_1e_1c"},
       {image:"https://oss-xpc0.xpccdn.com/newupload/assets/article/cover/2022/3/6245a0b74ce56@1125w_633h_1e_1c"},
       {image:"https://oss-xpc0.xpccdn.com/newupload/assets/article/cover/2022/3/62434328d0804@1125w_633h_1e_1c"},
       {image:"https://oss-xpc0.xpccdn.com/newupload/assets/article/cover/2022/3/623fb68a91cdb@1125w_633h_1e_1c"},
       {image:"https://oss-xpc0.xpccdn.com/newupload/assets/article/cover/2022/3/62454f789939e@1125w_633h_1e_1c"},
       {image:"https://oss-xpc0.xpccdn.com/newupload/assets/article/cover/2022/4/6246f5095e6b3@1125w_633h_1e_1c"},
       {image:"https://oss-xpc0.xpccdn.com/newupload/assets/article/cover/2022/3/624567365e04a@1125w_633h_1e_1c"}
      ],
      posts:null,
      postList:[]
    },
    onLoad(){
      wx.showLoading({
        title: '努力加载中...',
      })
      // request.get("/index/getBanner?type=9")
      // .then(re=>{
      //   // console.log("re==>",re)
      //   this.setData({
      //     banners:re.data
      //   });
      // });
      // request.get("/index/getIndexPosts").then(re=>{
      //   // console.log("re==>",re)
      //   this.setData({
      //     posts:re.data,
      //     postList:re.data.list
      //   });
      //   wx.hideLoading()
      // });
      request.get("https://app.xinpianchang.com/home/recommend")
      .then(re=>{
        console.log("首页推荐===>",re);
        wx.hideLoading();
        re.data.list.shift();
        this.setData({
          postList:re.data.list,
          posts:re.data
        })
      })
    },
    jumpsearch(){
        wx.navigateTo({
          url: '/pages/search/search',
        })
    },
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
console.log(this.data)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
      wx.showLoading({
        title: '努力加载中...',
      })
      // let postURL="/index/getIndexPosts?lastid="+this.data.posts.lastid;
      // request.get(postURL).then(re=>{
      //   let lists=re.data.list;//得到分页列表数组
      //   let postList=this.data.postList;
      //       postList=postList.concat(lists);//合并数组把加载到的数据新增到数组
      //       console.log(postList);
      //       this.setData({
      //         postList:postList,
      //         posts:re.data//把posts重新赋值
      //       });
      //       wx.hideLoading()
      //   // console.log('re====>',re)
      // })
      request.get("https://app.xinpianchang.com"+this.data.posts.next_page_url)
      .then(re=>{
        console.log("触底下一页内容",re);
        wx.hideLoading();
        //合并下一页的数组到显示的数组
        let postList=this.data.postList;
        let list=re.data.list;
        postList=postList.concat(list);
        this.setData({
          postList:postList,
          posts:re.data//重新赋值下一页内容
        })
      })
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
      return {
        title:"我的分享",
        imageUrl:"https://oss-xpc0.xpccdn.com/newupload/assets/article/cover/2022/2/621286b6e8fe3"
      }
    },
    onShareTimeline(){
      return {
        imageUrl:"https://oss-xpc0.xpccdn.com/newupload/assets/article/cover/2022/2/62038124918af"
      }
    }
})